'''
Created on May 21, 2013

@author: kieubinh
'''

from svmutil import *

#max = 100000
#number_train = 65000

import sys

name_file = sys.argv[1]

f_in = open(name_file,'r')
count = 0
for line in f_in:
    if len(line)>=5:
        count+=1

f_in.close()

y, x = svm_read_problem(name_file)


import time
start_time = time.time()
    
m = svm_train(y, x, '-s 0 -v 10 -h 0')
print m
print "The running time: "+str(time.time()-start_time)
print "---------------------------------------------"
ctest = int(count / 10)

for cfold in range(10):
    number_train = 0
    number_test = 0
    ytrain=[]
    xtrain=[]
    ytest=[]
    xtest=[]
    for i in range(count):
        if (i>=cfold*ctest and i<(cfold+1)*ctest):
            ytest.append(y[i])
            #ytest.
            xtest.append(x[i])
            number_test+=1
        else:
            ytrain.append(y[i])
            xtrain.append(x[i])
            number_train+=1
    print "Fold "+str(cfold+1)+" : "
    print "The number of training instances: "+str(len(ytrain))
    print "The number of testing instances: "+str(len(ytest))
    import time
    start_time = time.time()
    
    m = svm_train(ytrain, xtrain, '-s 0 -h 0')
    
    print "The running time of training: "+str(time.time()-start_time)
    #train
    #print "training result......................................................."
    
    #p_labels, p_acc, p_vals = svm_predict(ytrain, xtrain, m)
    
    #test
    print "testing result........................................................"
    p_labels, p_acc, p_vals = svm_predict(ytest, xtest, m)

    print "\n----------------------------------------------------------------------\n"
 
